# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms.dm.dm_design_vehicle_ontime_detail_dt import jms_dm__dm_design_vehicle_ontime_detail_dt

label = f"dws_design_vehicle_ontime_detail_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dws__dws_design_vehicle_ontime_detail_dt = RichSqlSensor(
    task_id='doris_jms_dws__dws_design_vehicle_ontime_detail_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='doris',
    pre_sql=f"""
            TRUNCATE TABLE jms_dws.dws_design_vehicle_ontime_detail_dt PARTITION (p{{{{ execution_date | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}}, p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}});
            LOAD LABEL jms_dws.{label} (
            DATA INFILE(
                        "hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_design_vehicle_ontime_detail_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
                        "hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_design_vehicle_ontime_detail_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
                        "hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dm.db/external/dm_design_vehicle_ontime_detail_dt/dt={{{{ execution_date | cst_ds }}}}/*")
            INTO TABLE dws_design_vehicle_ontime_detail_dt
            FORMAT AS 'PARQUET'
            ( datetime               
             ,shipment_no            
             ,vehicleline_name       
             ,vehicleline_code       
             ,line_name              
             ,total_mileage          
             ,task_type              
             ,operation_model        
             ,plate_number           
             ,vehicletype_name       
             ,planned_departure_time 
             ,actual_departure_time  
             ,ispunctuality          
             ,planned_arrival_time   
             ,actual_arrival_time    
             ,islate                 
             ,plan_driving_time      
             ,actual_driving_time    
             ,istimeout              
             ,timevalue                
             ,start_center_name       
             ,start_center_id         
             ,start_center_code       
             ,start_center            
             ,start_agent_id          
             ,start_agent_code        
             ,start_agent_name        
             ,start_regional_id       
             ,start_regional_desc     
             ,start_provider_id       
             ,start_provider_name     
             ,end_center_name         
             ,end_center_id           
             ,end_center_code         
             ,end_center              
             ,end_agent_id            
             ,end_agent_code          
             ,end_agent_name          
             ,end_regional_id         
             ,end_regional_desc       
             ,end_provider_id         
             ,end_provider_name                  
            )
            columns from path as (dt)
            set( datetime=datetime
                ,shipment_no=shipment_no
                ,vehicleline_name=vehicleline_name
                ,vehicleline_code=vehicleline_code
                ,line_name=line_name
                ,total_mileage=total_mileage
                ,task_type=task_type
                ,operation_model=operation_model
                ,plate_number=plate_number
                ,vehicletype_name=vehicletype_name
                ,planned_departure_time=planned_departure_time
                ,actual_departure_time=actual_departure_time
                ,ispunctuality=ispunctuality
                ,planned_arrival_time=planned_arrival_time
                ,actual_arrival_time=actual_arrival_time
                ,islate=islate
                ,plan_driving_time=plan_driving_time
                ,actual_driving_time=actual_driving_time
                ,istimeout=istimeout
                ,timevalue=timevalue             
                ,start_center_name=start_center_name
                ,start_center_id=start_center_id
                ,start_center_code=start_center_code
                ,start_center=start_center
                ,start_agent_id=start_agent_id
                ,start_agent_code=start_agent_code
                ,start_agent_name=start_agent_name
                ,start_regional_id=start_regional_id
                ,start_regional_desc=start_regional_desc
                ,start_provider_id=start_provider_id
                ,start_provider_name=start_provider_name
                ,end_center_name=end_center_name
                ,end_center_id=end_center_id
                ,end_center_code=end_center_code
                ,end_center=end_center
                ,end_agent_id=end_agent_id
                ,end_agent_code=end_agent_code
                ,end_agent_name=end_agent_name
                ,end_regional_id=end_regional_id
                ,end_regional_desc=end_regional_desc
                ,end_provider_id=end_provider_id
                ,end_provider_name=end_provider_name
                ,dt=dt
            )

    ) WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dws WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dws WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dws__dws_design_vehicle_ontime_detail_dt << jms_dm__dm_design_vehicle_ontime_detail_dt

